
package com.kugou.game.sdk.base;

import com.kugou.framework.base.BaseWorkerFragmentActivity;
import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.api.common.IEventCode;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.PlatformEventDispatcher;
import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.utils.IActivityIndex;
import com.kugou.game.sdk.utils.Rx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.WindowManager;

/**
 * 描述:SDK界面基类
 * 
 * @author chenys
 * @since 2013-12-12 上午9:41:46
 */
public class BaseSDKWorkerFragmentActivity extends BaseWorkerFragmentActivity implements
        IActivityIndex {
    /**
     * 返回游戏关闭页面广播处理类
     */
    private ExitReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SDKControler.activityCreate();
        receiver = new ExitReceiver();
        IntentFilter filter = new IntentFilter(SDKActions.ACTION_BACK_TO_GAME);
        registerReceiver(receiver, filter);
        // 重新加载页面时，恢复数据
        if (savedInstanceState != null) {
            User user = (User) savedInstanceState.getSerializable("user");
            if (user != null) {
                UserManager.getInstance().setCurrentUser(user);
            }
        }

        if (SDKControler.isInitialize()) {
            setTheme(Rx.style.kg_SdkTheme);
            if (SDKControler.isFullScreen()) {
                // 设置全屏
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
            if (SDKControler.getActivityOrientation() == ActivityOrientation.PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else {// 如果屏幕之前是竖屏，则设置为横屏；如果已经是横屏，则重新设置两次，使屏幕旋转360度保持横屏不受重力影响
                // int orientation =
                // getResources().getConfiguration().orientation;
                // Log.i("sdk_orientation", orientation == 1 ? "竖屏" : "横屏");
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

                // if (getResources().getConfiguration().orientation ==
                // Configuration.ORIENTATION_PORTRAIT) {
                // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                // } else {
                // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                // }
            }
        } else {
            throw new RuntimeException("sdk还没有初始化.");
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // super.onSaveInstanceState(outState);
        User user = UserManager.getInstance().getCurrentUserInternal();
        if (user != null) {
            outState.putSerializable("user", user);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SDKControler.activityDestory();
        if (SDKControler.getActivityCounter() == 0) {
            PlatformEventDispatcher.sendEvent(IEventCode.GO_BACK_TO_GAME, null);
        }
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    /**
     * 描述:关闭子页面回到游戏广播接收器
     * 
     * @author liux
     * @since 2013-12-18 下午05:22:34
     */
    class ExitReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null && action.equals(SDKActions.ACTION_BACK_TO_GAME)) {
                finish();
            }
        }
    }
}
